home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / vectlib.exe / VECT2.H < prev    next >
C/C++ Source or Header  |  1991-07-25  |  4KB  |  130 lines

  1. // Vect2.h, By Scott Litvinoff
  2. // Class definition for 2-d vectors
  3. // All angles are relative to east, with positive angles being in the northern
  4. // direction and negative angles being in the southern direction
  5.  
  6. #include "stdlib.h"
  7. #include "float.h"
  8. #include "iostream.h"
  9. #include "math.h"
  10.  
  11. #define Polar '1'
  12. #define Rect '2'
  13.  
  14. class vector2
  15. {
  16.     double mag,ang,x,y;
  17.  
  18.     public:
  19.     vector2(char inrad='0',char type='1',double part1=0,double part2=0)
  20.     {
  21.         double dummy1,dummy2,dummy3;   //Constructor
  22.         if(type==Polar)                //Vector can be constructed in rectangular
  23.         {                              //or polar form, whenever constructing a
  24.             mag=part1;                 //vector in polar form, if the vector's
  25.             dummy1=fmod(((part2 * 2 * M_PI) / 360) , (2 * M_PI));
  26.             if(inrad=='0')             //angle is in degrees then set the first
  27.             {                          //parameter to '0', if it's in radians
  28.                 dummy2=cos(dummy1)*mag; //set it to '1',the angle is stored in
  29.                 dummy3=sin(dummy1)*mag; //radians, the second parameter should
  30.             }                          //be either Polar if it's polar, or
  31.             else                       //Rect if it's rectangular
  32.             {                          //If polar, part1 is magnitude and
  33.                 dummy2=cos(part2)*mag; //part2 is angle, if rectangular, part1
  34.                 dummy3=sin(part2)*mag; //is x and part2 is y
  35.             }
  36.             x=dummy2;                  //Vector is stored in both forms internally
  37.             y=dummy3;
  38.             if(inrad=='1')
  39.             ang=part2;
  40.             else
  41.             ang=dummy1;
  42.             return;
  43.         }
  44.         if(type==Rect)
  45.         {
  46.             x=part1;
  47.             y=part2;
  48.             if(x>0)
  49.             dummy1=atan(part2/part1);
  50.             else
  51.             dummy1=M_PI/2;
  52.             dummy2=sqrt((part1*part1)+(part2*part2));
  53.             ang=dummy1;
  54.             mag=dummy2;
  55.             return;
  56.         }
  57.     }
  58.     friend vector2 operator+(vector2&,vector2&);   // Vector binary addition
  59.  
  60.     friend vector2 operator*(vector2&,vector2&);   // Vector binary multiplication
  61.  
  62.     friend vector2 operator/(vector2&,vector2&);   // Vector binary division
  63.  
  64.     friend vector2 operator-(vector2&,vector2&);   // Vector binary subtraction
  65.  
  66.     friend vector2 operator-(vector2&);           // Unary vector negation
  67.  
  68.     friend vector2 operator*(vector2&,double);    // Scalar multiplication of vector
  69.  
  70.     friend vector2 operator*(double,vector2&);    // Scalar multiplication of vector
  71.  
  72.     friend double operator|(vector2&,vector2&);   // Vector dot product
  73.  
  74.     void print(char which)           //Outputs vector in either form
  75.     {                                //If which is set to Rect then outputs
  76.         double dummy;                //In rectangular form, if Polar, then
  77.         if(which==Rect)              //Outputs in polar, if in polar, outputs
  78.         {                            //angle in degrees
  79.             if(x < 1e-4)
  80.             cout<<'0';
  81.             else
  82.             cout<<x;
  83.             cout<<",";
  84.             if(y < 1e-4)
  85.             cout<<'0';
  86.             else
  87.             cout<<y;
  88.             return;
  89.         }
  90.         if(which==Polar)
  91.         {
  92.             dummy=fmod(((360 * ang) / (2 * M_PI)) , 360);
  93.             if(mag < 1e-4)
  94.             cout<<'0';
  95.             else
  96.             cout<<mag;
  97.             cout<<",";
  98.             if(dummy < 1e-4)
  99.             cout<<'0';
  100.             else
  101.             cout<<dummy;
  102.             cout<<"°";
  103.             return;
  104.         }
  105.         return;
  106.     }
  107.     double xp(vector2 &__dude)
  108.     {
  109.         return(__dude.x);             //Returns x coordinate of vector
  110.     }
  111.     double yp(vector2 &__dude)
  112.     {
  113.         return(__dude.y);             //Returns y coordinate of vector
  114.     }
  115.     double magp(vector2 &__dude)
  116.     {
  117.         return(__dude.mag);           //Returns magnitude of vector
  118.     }
  119.     double angr(vector2 &__dude)
  120.     {
  121.         return(__dude.ang);           //Returns angle of vector in radians
  122.     }
  123.     double angd(vector2 &__dude)
  124.     {
  125.         double dummy;                 //Returns angle of vector in degrees
  126.         dummy=fmod(((360 * __dude.ang) / (2 * M_PI)) , 360);
  127.         return(dummy);
  128.     }
  129. };
  130.